package com.hand.wfive.view.countdowntime;

import android.content.Context;
import android.graphics.Typeface;
import android.os.CountDownTimer;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;

public abstract class BaseCountDownTimerView extends LinearLayout {

    private Context mContext;
    public static final int TYPE_LABEL_TEXT = 1001;
    public static final int TYPE_LABEL_COLON = 1002;
    public static final int TYPE_LABEL_COLON1 = 1003;

    /**
     * 倒计时控制器
     */
    private CountDownTimer mCountDownTimer;

    private OnCountDownTimerListener OnCountDownTimerListener;
    private OnCountDownTimerLastListener OnCountDownTimerlastListener;

    private long mMillis;
    private long mCurrentMills; //当前剩余多少秒
    private long LastMills; //当前剩余多少秒

    /**
     * 时
     */
    private TextView mHourTextView;

    /**
     * 分
     */
    private TextView mMinTextView;

    /**
     * 秒
     */
    private TextView mSecondTextView;

    /**
     * 获取边框颜色
     *
     * @return
     */
    protected abstract int getStrokeColor();

    protected abstract int getStrokepadingV();

    protected abstract int getStrokepadingH();

    /**
     * 设置背景色
     *
     * @return
     */
    protected abstract int getBackgroundColor();

    /**
     * 获取文字颜色
     *
     * @return
     */
    protected abstract int getLabelTextColor();

    protected abstract int getSeparatorTextColor();

    protected abstract int getSeparatorPaddingHor();

    /**
     * 获取边框圆角
     *
     * @return
     */
    protected abstract int getCornerRadius();


    /**
     * 获取是否加粗
     *
     * @return
     */
    protected abstract boolean getTimetextisbold();

    /**
     * 获取单位文本展示
     *
     * @return
     */
    protected abstract int getLabelType();

    /**
     * 获取标签文字大小
     *
     * @return
     */
    protected abstract float getTextSize();

    protected abstract float getSeparatorTextSize();

    public BaseCountDownTimerView(Context context, AttributeSet attrs,
                                  int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
//        init();
    }

    public BaseCountDownTimerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BaseCountDownTimerView(Context context) {
        this(context, null);
    }

    protected void init() {
        this.setOrientation(HORIZONTAL);// 设置布局排列方式
        createView();// 创造三个标签
        if (getLabelType() == TYPE_LABEL_TEXT) {
            addLabelViewForText();// 添加标签到容器中
        } else {
            addLabelView();// 添加标签到容器中
        }
    }

    /**
     * 创建时、分、秒的标签
     */
    private void createView() {
        mHourTextView = createLabel();
        mMinTextView = createLabel();
        mSecondTextView = createLabel();
    }

    /**
     * 添加标签到容器中
     */
    private void addLabelView() {
        removeAllViews();
        this.addView(mHourTextView);
        this.addView(createColon());
        this.addView(mMinTextView);
        this.addView(createColon());
        this.addView(mSecondTextView);
    }

    /**
     * 添加标签到容器中
     */
    private void addLabelViewForText() {
        removeAllViews();
        this.addView(mHourTextView);
        this.addView(createText("时"));
        this.addView(mMinTextView);
        this.addView(createText("分"));
        this.addView(mSecondTextView);
        this.addView(createText("秒"));
    }

    /**
     * 创建冒号
     *
     * @return
     */
    private TextView createColon() {
        TextView textView = new TextView(mContext);
        textView.setTextColor(getSeparatorTextColor());
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getSeparatorTextSize());
        textView.setText(":");
        textView.setGravity(Gravity.CENTER);
        textView.setPadding(getSeparatorPaddingHor(), 0, getSeparatorPaddingHor(), 0);
        return textView;
    }

    /**
     * 创建单位文本
     *
     * @return
     */
    private TextView createText(String text) {
        TextView textView = new TextView(mContext);
        textView.setTextColor(getSeparatorTextColor());
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getSeparatorTextSize());
        if (getTimetextisbold()) {
            SpannableString spannableString = new SpannableString(text);
            StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
            spannableString.setSpan(boldSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(spannableString);
        } else {
            textView.setText(text);
        }

        textView.setGravity(Gravity.CENTER);
        textView.setPadding(getSeparatorPaddingHor(), 0, getSeparatorPaddingHor(), 0);
        return textView;
    }

    /**
     * 创建标签
     *
     * @return
     */
    private TextView createLabel() {

        TextView textView = new GradientTextView(mContext, getLabelType(), getStrokepadingV(), getStrokepadingH())
                .setTextColor(getLabelTextColor())
                .setStrokeColor(getStrokeColor())
//                .setTextpading(getStrokepadingV(),getStrokepadingH())
                .setBackgroundColor(getBackgroundColor())
                .setTextSize(getTextSize()).setStrokeRadius(getCornerRadius())
                .build();

        return textView;
    }

    /**
     * 创建倒计时
     */
    private void createCountDownTimer() {
        mCountDownTimer = new CountDownTimer(mMillis, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                mCurrentMills = millisUntilFinished;
                setSecond(millisUntilFinished);// 设置秒
//                dealTime(millisUntilFinished / 1000);// 设置秒
            }

            @Override
            public void onFinish() {
                OnCountDownTimerListener.onFinish();
            }
        };
    }

    /**
     * 设置秒
     *
     * @param millis
     */
    private void setSecond(long millis) {
        long totalSeconds = millis / 1000;
        String second = (int) (totalSeconds % 60) + "";// 秒
        long totalMinutes = totalSeconds / 60;
        String minute = (int) (totalMinutes % 60) + "";// 分
      /*  long totalHours = totalMinutes / 60;
        String hour = (int) (totalHours % 24) + "";// 时*/
        long totalHours1 = totalMinutes / 60;
        String hour1 = (int) (totalHours1) + "";// 时
        Log.i("TAG", "hour:" + hour1);
        Log.i("TAG", "minute:" + minute);
        Log.i("TAG", "second:" + second);
        if (hour1.length() == 1) {
            hour1 = "0" + hour1;
        }
       /* if (hour.length() == 1) {
            hour = "0" + hour;
        }*/
        if (minute.length() == 1) {
            minute = "0" + minute;
        }
        if (second.length() == 1) {
            second = "0" + second;
        }
        mHourTextView.setText(hour1);
//        mHourTextView.setText(hour);
        mMinTextView.setText(minute);
        mSecondTextView.setText(second);
        if (totalSeconds == LastMills / 1000) {
            Log.i("8989", "setSecond: ");
            if (OnCountDownTimerlastListener != null) {
                OnCountDownTimerlastListener.onLastTimeListener();
            }
        }
    }

    /**
     * 设置监听事件
     *
     * @param listener
     */
    public void setDownTimerListener(OnCountDownTimerListener listener) {
        this.OnCountDownTimerListener = listener;
    }

    /**
     * 设置倒计时监听事件
     *
     * @param listener
     */
    public void setDownTimerLastListener(OnCountDownTimerLastListener listener) {
        this.OnCountDownTimerlastListener = listener;
    }

    /**
     * 设置时间值
     *
     * @param millis
     */
    public void setDownTime(long millis) {
        this.mMillis = millis;
    }

    /**
     * 设置最后时间值提示
     *
     * @param millis
     */
    public void setDownlastTime(long millis) {
        this.LastMills = millis;
    }

    /**
     * 开始倒计时
     */
    public void startDownTimer() {
        createCountDownTimer();// 创建倒计时
        if (mCountDownTimer != null)
            mCountDownTimer.start();
    }

    public void cancelDownTimer() {
        if (mCountDownTimer != null)
            mCountDownTimer.cancel();
    }

    public long getCurrentMills() {
        return mCurrentMills;
    }


    @Override
    public void setAlpha(float alpha) {
        super.setAlpha(alpha);
    }
}
